<?php

namespace app\api\controller;

use app\common\model\ChatPromptSearch;
use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 聊天提示词
 *
 * api-ChatPrompt
 *
 * @Apidoc\Title("聊天提示词")
 * @Apidoc\Group("api")
 */
class ChatPrompt extends Auth
{
    /**
     * @Apidoc\Title("提示词分类+列表")
     * list
     */
    public function list()
    {
        $keyword = input('keywords', '', 'trim');
        $cats = \app\common\model\ChatPromptClassification::order('sort', 'asc')->select()->toArray();
        foreach ($cats as $index => $cat){
                $cats[$index]['count'] = \app\common\model\ChatPrompt::where('chat_prompt_classification_id', $cat['id'])
                    ->where('name|chat_prompt_front', 'like', "%{$keyword}%")
                    ->count();
                $cats[$index]['lists'] = [];
        }

        $chat_prompt_ids = \app\common\model\Chat::where('member_id', $this->request->uid)
            ->where('chat_prompt_id', '>', 0)
            ->group('chat_prompt_id')
            ->column('chat_prompt_id');

        $fav = \app\common\model\ChatPrompt::where('id', 'in', $chat_prompt_ids)->count();


        array_push($cats, [
            'id' => -1,
            'count' => $fav,
            'lists' => []
        ]);

        $count_all =    \app\common\model\ChatPrompt::where('name|chat_prompt_front', 'like', "%{$keyword}%")->count();
        array_push($cats, [
            'id' => 0,
            'count' => $count_all,
            'lists' => []
        ]);


        if($keyword) {
            $record = ChatPromptSearch::where('name', $keyword)->find();
            if($record) {
                $record->num = $record->num + 1;
                $record->matching_num = $count_all;
                $record->save();
            }else{
                ChatPromptSearch::create([
                    'name' => $keyword,
                    'num' => 1,
                    'matching_num' => $count_all
                ]);
            }
        }

        return success('', $cats);
    }


    /**
     * @Apidoc\Title("根据分类获取列表")
     * getListByClass
     */
    public function getListByClass(): Json
    {
        $id = input('id');
        $keyword = input('keywords', '', 'trim');
        $data = [];
        if($id == -1 ) {
            $commonUse = \app\common\model\Chat::where('member_id', $this->request->uid)
                ->where('chat_prompt_id', '>', 0)
                ->field('chat_prompt_id, count(chat_prompt_id) as count')
                ->order('count', 'desc')
                ->group('chat_prompt_id')
                ->select();
            $data = [];
            foreach ($commonUse as $item) {
                $chatPrompt   =  \app\common\model\ChatPrompt::find($item['chat_prompt_id']);
                if($chatPrompt) {
                    $data[] = $chatPrompt;
                }

            }
        }else{
            $res = \app\common\model\ChatPrompt::order('sort', 'asc');
            if($id > 0){
                $res= $res->where('chat_prompt_classification_id', input('id'));
            }
            $res = $res->where('name|chat_prompt_front', 'like', "%{$keyword}%");
            $res = $res->paginate(40);
            $data = $res->toArray()['data'];
            foreach($data as $key=>$v){
                $data[$key]['name']=str_replace($keyword,"<b>".$keyword."</b>",$data[$key]['name']);
                $data[$key]['chat_prompt_front']=str_replace($keyword,"<b>".$keyword."</b>",$data[$key]['chat_prompt_front']);
            }
        }
        return success('', $data);
    }
}
